<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客系统</title>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/style.css">
    <!-- 引入Bootstrap CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.2.3/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入Font Awesome图标 -->
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.2.0/css/all.min.css" rel="stylesheet">
    <style>
        .header-search-form .form-control {
            height: 44px;
            font-size: 16px;
        }
        .header-search-form .btn {
            min-width: 90px;
            height: 44px;
            font-size: 16px;
            display: flex;
            align-items: center;
            justify-content: center;
            white-space: nowrap;
            padding: 0 18px;
        }
    </style>
</head>
<body>
    <header>
        <nav class="navbar navbar-expand-lg navbar-dark bg-primary">
            <div class="container">
                <a class="navbar-brand" href="${pageContext.request.contextPath}/blog/list">博客系统</a>
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarNav" aria-controls="navbarNav" aria-expanded="false" aria-label="Toggle navigation">
                    <span class="navbar-toggler-icon"></span>
                </button>
                <div class="collapse navbar-collapse" id="navbarNav">
                    <ul class="navbar-nav me-auto">
                        <li class="nav-item">
                            <a class="nav-link" href="${pageContext.request.contextPath}/blog/list">首页</a>
                        </li>
                        <li class="nav-item">
                            <a class="nav-link" href="${pageContext.request.contextPath}/blog/hot">热门博客</a>
                        </li>
                        <c:if test="${sessionScope.user != null}">
                            <li class="nav-item">
                                <a class="nav-link" href="${pageContext.request.contextPath}/blog/myblogs">我的博客</a>
                            </li>
                            <li class="nav-item">
                                <a class="nav-link" href="${pageContext.request.contextPath}/blog/new">写博客</a>
                            </li>
                            <c:if test="${sessionScope.user.role eq 'admin'}">
                                <li class="nav-item">
                                    <a class="nav-link" href="${pageContext.request.contextPath}/admin/" style="color: #ffcc00;">
                                        <i class="fas fa-cogs me-1"></i>后台管理
                                    </a>
                                </li>
                            </c:if>
                        </c:if>
                    </ul>
                    
                    <!-- 搜索框 -->
                    <form class="d-flex me-3 header-search-form" action="${pageContext.request.contextPath}/blog/search" method="post">
                        <input class="form-control me-2" type="search" name="keyword" placeholder="搜索博客..." aria-label="Search">
                        <button class="btn btn-outline-light" type="submit">搜索</button>
                    </form>
                    
                    <div class="navbar-nav">
                        <c:choose>
                            <c:when test="${sessionScope.user != null}">
                                <div class="nav-item dropdown">
                                    <a class="nav-link dropdown-toggle" href="#" id="navbarDropdown" role="button" data-bs-toggle="dropdown" aria-expanded="false">
                                        <i class="fas fa-user me-1"></i>${sessionScope.user.nickname != null && sessionScope.user.nickname != '' ? sessionScope.user.nickname : sessionScope.user.username}
                                    </a>
                                    <ul class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/user/view?id=${sessionScope.user.id}">个人中心</a></li>
                                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/user/edit?id=${sessionScope.user.id}">修改资料</a></li>
                                        <c:if test="${sessionScope.user.role eq 'admin'}">
                                            <li><hr class="dropdown-divider"></li>
                                            <li><a class="dropdown-item" href="${pageContext.request.contextPath}/admin/">
                                                <i class="fas fa-cogs me-1"></i>后台管理
                                            </a></li>
                                        </c:if>
                                        <li><hr class="dropdown-divider"></li>
                                        <li><a class="dropdown-item" href="${pageContext.request.contextPath}/user/logout">退出登录</a></li>
                                    </ul>
                                </div>
                            </c:when>
                            <c:otherwise>
                                <a class="nav-link" href="${pageContext.request.contextPath}/user/login">登录</a>
                                <a class="nav-link" href="${pageContext.request.contextPath}/user/register">注册</a>
                            </c:otherwise>
                        </c:choose>
                    </div>
                </div>
            </div>
        </nav>
    </header>
    
    <div class="container mt-4">
        <!-- 显示提示消息 -->
        <c:if test="${not empty message}">
            <div class="alert alert-success alert-dismissible fade show" role="alert">
                ${message}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        </c:if>
        
        <!-- 显示错误消息 -->
        <c:if test="${not empty error}">
            <div class="alert alert-danger alert-dismissible fade show" role="alert">
                ${error}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        </c:if> 